import { createStore, applyMiddleware, compose } from 'redux'
// import thunkMiddleware from 'redux-thunk'
import createSagaMiddleware from 'redux-saga'
import { persistStore, persistReducer } from 'redux-persist'
import rootReducer from '@REDUCERS'
import rootSagas from '@SAGAS'
import Storage from './storage'

const composeEnhancers = typeof window === 'object'
  && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
  }) : compose

const middlewares = [
  // thunkMiddleware
]
const sagaMiddleware = createSagaMiddleware()

// if (process.env.NODE_ENV === 'development' && process.env.TARO_ENV !== 'quickapp') {
middlewares.push(require('redux-logger').createLogger())
// }

const enhancer = composeEnhancers(
  applyMiddleware(sagaMiddleware, ...middlewares),
  // other store enhancers if any
)

const persistConfig = {
  key: 'root',
  storage: Storage,
  whitelist: [
    'profile', 'chat', 'message', 'consultation',
    'doctor', 'feed', 'patient', 'myDoctor', 'decision',
    'video', 'servicePack', 'tag', 'smartAssistant', 'meditation',
    'managementPlan', 'dataCenter', 'address', 'medRecord'
  ],
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

export default function configStore() {
  const store = createStore(persistedReducer, enhancer)
  sagaMiddleware.run(rootSagas)
  const persistor = persistStore(store)
  return { store, persistor }
}
